package pages;

import constant.ExceptionConstant;
import constant.MessageConstant;
import constant.PatternConstant;
import pojo.Admin;
import utils.JDBCUtil;
import utils.RegisterUtil;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

public class IndexPage {
    private static final Scanner scanner = new Scanner(System.in);

    //数据缓存，缓存数据库中的所有管理员数据
    private static final List<Admin> listAdmin;

    static {
        try {
            listAdmin = JDBCUtil.selectAdmin();
        } catch (SQLException e) {
            throw new RuntimeException(ExceptionConstant.CACHE_GET_EXCEPTION);
        }
    }

    public IndexPage() {
        mainPage();
    }

    private static void mainPage() {
        while (true) {
            System.out.println("===== 主页面 =====");
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.println("3. 修改密码");
            System.out.println("4. 退出");
            System.out.println("=================");
            System.out.print("请输入你的选择: ");
            String choice = scanner.next();

            switch (choice) {
                case "1":
                    login();
                    return;
                case "2":
                    try {
                        register();
                    } catch (SQLException e) {
                        throw new RuntimeException(ExceptionConstant.SQL_EXECUTE_EXCEPTION);
                    }
                    return;
                case "3":
                    try {
                        updatePassword();
                    } catch (SQLException e) {
                        throw new RuntimeException(ExceptionConstant.SQL_EXECUTE_EXCEPTION);
                    }
                    return;
                case "4":
                    System.exit(0);
                default:
                    System.err.println(MessageConstant.NOT_CHOICE);
            }
        }
    }

    /**
     * 登录
     */
    private static void login() {
        //获取用户名和密码
        System.out.println("请输入用户名: ");
        String username = scanner.next();
        System.out.println("请输入密码: ");
        String password = JDBCUtil.encryptionPassword(scanner.next());

        //数据校验
        int index = RegisterUtil.getIndex(listAdmin, username);
        if (index == -1 || !listAdmin.get(index).getPassword().equals(password)) {
            System.err.println(MessageConstant.LOGIN_FAIL);
            mainPage();
            return;
        }

        //数据校验成功进入学生管理页面
        System.out.println("登录成功");
        new StudentPage();
    }

    /**
     * 注册
     */
    private static void register() throws SQLException {
        //创建用户对象保存用户注册的数据
        Admin newAdmin = new Admin();

        //注册用户名
        while (true) {
            System.out.println("请输入用户名：");
            String username = scanner.next();
            if (RegisterUtil.getIndex(listAdmin, username) >= 0) {
                System.err.println(MessageConstant.USERNAME_ALREADY_EXIST);
                continue;
            } else if (!username.matches(PatternConstant.USERNAME_PATTERN)) {
                System.err.println(MessageConstant.USERNAME_FORMAT_ERROR);
                continue;
            }
            newAdmin.setUsername(username);
            break;
        }

        //注册密码
        while (true) {
            System.out.println("请输入密码：");
            String password = scanner.next();
            if (!password.matches(PatternConstant.PASSWORD_PATTERN)) {
                System.err.println(MessageConstant.PASSWORD_FORMAT_ERROR);
                continue;
            }
            newAdmin.setPassword(JDBCUtil.encryptionPassword(password));
            break;
        }

        //获取验证码
        String captcha = RegisterUtil.getCAPTCHA();
        while (true) {
            System.out.println("您的验证码为：" + captcha);
            System.out.println("请输入验证码：");
            String input = scanner.next();
            if (!input.equalsIgnoreCase(captcha)) {
                System.err.println(MessageConstant.CAPTCHA_ERROR);
                continue;
            }
            break;
        }

        //注册成功，进入主页面
        newAdmin.setCreate_time(LocalDateTime.now());
        newAdmin.setUpdate_time(LocalDateTime.now());
        JDBCUtil.insert(newAdmin);
        listAdmin.add(newAdmin);
        System.out.println("注册成功");
        mainPage();
    }

    /**
     * 修改密码
     */
    private static void updatePassword() throws SQLException {
        //创建用户对象保存用户输入的数据
        Admin admin = new Admin();

        //获取并校验用户名
        while (true) {
            System.out.println("请输入用户名：");
            String username = scanner.next();
            if (RegisterUtil.getIndex(listAdmin, username) == -1) {
                System.err.println(MessageConstant.USERNAME_NOT_ALREADY);
                continue;
            }
            admin.setUsername(username);
            break;
        }

        //获取并校验密码
        while (true) {
            System.out.println("请输入新密码：");
            String password = scanner.next();
            if (!password.matches(PatternConstant.PASSWORD_PATTERN)) {
                System.err.println(MessageConstant.PASSWORD_FORMAT_ERROR);
                continue;
            }
            System.out.println("请确认密码：");
            String ackPassword = scanner.next();
            if (!ackPassword.equals(password)) {
                System.err.println(MessageConstant.PASSWORD_NOT_EQUALS);
                continue;
            }
            admin.setPassword(password);
            break;
        }

        //将新的信息保存到数据库和本地缓存
        admin.setUpdate_time(LocalDateTime.now());
        JDBCUtil.update(admin);
        listAdmin.get(RegisterUtil.getIndex(listAdmin, admin.getUsername())).setPassword(JDBCUtil.encryptionPassword(admin.getPassword()));
        //修改时间程序用不到，所以没必要更新listAdmin中的update_time
        System.out.println("修改密码成功");
        mainPage();
    }
}